package com.oilsection.demo.service.impl;

import com.oilsection.demo.dao.*;
import com.oilsection.demo.po.JkxlInfoTable;
import com.oilsection.demo.po.JzInfoTable;
import com.oilsection.demo.po.KqInfoTable;
import com.oilsection.demo.po.QkoverviewinfoTable;
import com.oilsection.demo.entity.indexInfo;
import com.oilsection.demo.service.IndexListService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author yangyuge
 * @description
 * @Date 2024/5/3 16:12
 */
@Service
public class IndexListServiceImpl implements IndexListService {

    private static final Logger logger = LoggerFactory.getLogger(IndexListServiceImpl.class);
    @Autowired
    private GetIndexListSourceDao getIndexListSourceDao;
    @Autowired
    private GetQkListSourceDao getQkListSourceDao;
    @Autowired
    private GetJkxlListSourceDao getJkxlListSourceDao;
    @Autowired
    private GetJzListSourceDao getJzListSourceDao;
//    {
//        "kqNum": 2,
//            "kqArea": 156125.0,
//            "jzNum": 32,
//            "jcdNum": 78,
//            "kqxlNum": 82,
//            "cqxlNum": 4,
//            "hkxlNum": 4,
//            "jkxlNum": 11,
//            "kqList": [
//        {
//            "name": "金坛库区",
//                "updateTime": "2023年06月07日"
//        },
//        {
//            "name": "刘庄库区",
//                "updateTime": "2023年06月07日"
//        }
//    ],
//        "kqxlList": [
//        {
//            "name": "金坛库区",
//                "value": "55"
//        },
//        {
//            "name": "刘庄库区",
//                "value": "27"
//        }
//    ],
//        "cqxlList": [
//        {
//            "name": "金坛库区",
//                "value": "0"
//        },
//        {
//            "name": "刘庄库区",
//                "value": "4"
//        }
//    ],
//        "hkxlList": [
//        {
//            "name": "金坛库区",
//                "value": "4"
//        },
//        {
//            "name": "刘庄库区",
//                "value": "0"
//        }
//    ],
//        "kqInfoList": null,
//            "jkxlList": [
//        {
//            "name": "金坛库区",
//                "value": "9"
//        },
//        {
//            "name": "刘庄库区",
//                "value": "1"
//        }
//    ]
//    }

    @Override
    public indexInfo getIndexList() {
        List<QkoverviewinfoTable> qkoverviewinfoTable = getIndexListSourceDao.findAll();
        List<KqInfoTable> kqInfoTable = getQkListSourceDao.findAll();
        List<JkxlInfoTable> jkxlInfoTables = new ArrayList<>();
        try{
            jkxlInfoTables =  getJkxlListSourceDao.findByDealSection("未处理");
        }catch (Exception e){}
        int jkxlLen = 0;
        try{
            jkxlLen = jkxlInfoTables.size();
        }catch(Exception e){
            jkxlLen = 0;
        }
        indexInfo result = new indexInfo();
        List<Map<String, String>> kqXlList = new ArrayList<>();
        List<Map<String, String>> ckXlList = new ArrayList<>();
        List<Map<String, String>> hkXlList = new ArrayList<>();
        List<Map<String, String>> kqList = new ArrayList<>();
        List<Map<String,String>> jkxlList = new ArrayList<>();
        result.setKqNum(qkoverviewinfoTable.get(0).getKqNum());
        result.setKqArea(qkoverviewinfoTable.get(0).getKqArea());
        result.setJcdNum(qkoverviewinfoTable.get(0).getJcdNum());
        result.setJzNum(qkoverviewinfoTable.get(0).getJzNum());
        result.setKqxlNum(qkoverviewinfoTable.get(0).getQkxlNum());
        result.setCqxlNum(qkoverviewinfoTable.get(0).getCqxlNum());
        result.setHkxlNum(qkoverviewinfoTable.get(0).getHkxlNum());
        result.setJkxlNum(jkxlLen);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        for (int i=0;i<kqInfoTable.size();i++) {
            KqInfoTable ele = kqInfoTable.get(i);
            Map<String, String> map = new HashMap<>();
            Map<String, String> mapKq = new HashMap<>();
            Map<String, String> mapCq = new HashMap<>();
            Map<String, String> mapHk = new HashMap<>();
            String d = "";
            if (ele.getUpdateTime() !=null && !"".equals(ele.getUpdateTime())){
                d = sdf.format(ele.getUpdateTime());
            }
            map.put("name", ele.getName());
            map.put("updateTime", d);
            mapKq.put("name", ele.getName());
            mapKq.put("value", String.valueOf(ele.getXlkNum()));
            mapCq.put("name", ele.getName());
            mapCq.put("value", String.valueOf(ele.getCqkNum()));
            mapHk.put("name", ele.getName());
            mapHk.put("value", String.valueOf(ele.getHkNum()));
            kqList.add(map);
            kqXlList.add(mapKq);
            ckXlList.add(mapCq);
            hkXlList.add(mapHk);

            Map<String,String> MapJk = new HashMap<>();
            int jkNum = 0;
            List<JzInfoTable> jzInfoTables = new ArrayList<>();
            try{
                jzInfoTables = getJzListSourceDao.findByKqId(ele.getId());
            }catch (Exception e){}

            for (int j =0;j<jzInfoTables.size();j++){
                JzInfoTable item = jzInfoTables.get(j);
                List<JkxlInfoTable> jkxlInfoTables1 = new ArrayList<>();
                jkxlInfoTables1 = getJkxlListSourceDao.findByXljzIdAndDealSection(item.getId(),"未处理");
                jkNum += jkxlInfoTables1.size();
            }
            MapJk.put("name",ele.getName());
            MapJk.put("value",String.valueOf(jkNum));
            jkxlList.add(MapJk);
        }

        result.setKqList(kqList);
        result.setKqxlList(kqXlList);
        result.setCqxlList(ckXlList);
        result.setHkxlList(hkXlList);
        result.setJkxlList(jkxlList);
        return result;
    }
}
